import { MemberLevel } from '@prisma/client';

/**
 * 会员等级相关工具函数
 */

// 会员等级显示名称映射
export const MEMBER_LEVEL_NAMES: Record<MemberLevel, string> = {
  regular: '일반회원',
  silver: '실버',
  gold: '골드', 
  diamond: '다이아',
  vip: 'VIP'
};

// 会员等级权重（用于比较等级高低）
export const MEMBER_LEVEL_WEIGHTS: Record<MemberLevel, number> = {
  regular: 0,
  silver: 1,
  gold: 2,
  diamond: 3,
  vip: 4
};

/**
 * 获取会员等级显示名称
 */
export function getMemberLevelName(level: MemberLevel): string {
  return MEMBER_LEVEL_NAMES[level];
}

/**
 * 检查用户是否有提现权限
 * 只有다이아(diamond)等级及以上可以提现
 */
export function canWithdraw(level: MemberLevel): boolean {
  return MEMBER_LEVEL_WEIGHTS[level] >= MEMBER_LEVEL_WEIGHTS.diamond;
}

/**
 * 比较两个会员等级的高低
 * @param level1 等级1
 * @param level2 等级2
 * @returns 1: level1 > level2, 0: level1 = level2, -1: level1 < level2
 */
export function compareMemberLevel(level1: MemberLevel, level2: MemberLevel): number {
  const weight1 = MEMBER_LEVEL_WEIGHTS[level1];
  const weight2 = MEMBER_LEVEL_WEIGHTS[level2];
  
  if (weight1 > weight2) return 1;
  if (weight1 < weight2) return -1;
  return 0;
}

/**
 * 获取所有会员等级列表（按等级从低到高排序）
 */
export function getAllMemberLevels(): Array<{ value: MemberLevel; name: string; weight: number }> {
  return Object.entries(MEMBER_LEVEL_NAMES).map(([value, name]) => ({
    value: value as MemberLevel,
    name,
    weight: MEMBER_LEVEL_WEIGHTS[value as MemberLevel]
  })).sort((a, b) => a.weight - b.weight);
}

/**
 * 验证会员等级是否有效
 */
export function isValidMemberLevel(level: string): level is MemberLevel {
  return Object.values(MemberLevel).includes(level as MemberLevel);
}
